﻿using System;
using System.Data; 
using System.Web;
//using System.Runtime.Caching;
using System.Runtime.Remoting.Messaging;
using System.Transactions;

namespace msBsc.AppLogic.Cfg
{ 

    public class CodeCallback
    {
        public CodeCallback()
        {
        }

        public void MakeException(object state)
        {
            throw new Exception("该方法抛出一个异常");
        }
        public static System.Threading.WaitCallback Wrap(System.Threading.WaitCallback callback, object[] state)
        {
            return new System.Threading.WaitCallback(
                delegate
                {
                    try
                    {
                        if (state[0] != null)
                        {
                            System.Web.HttpContext.Current = (System.Web.HttpContext)state[0];
                        }
                        callback(33);
                    }
                    finally
                    {
                        System.Web.HttpContext.Current = null;
                    }
                });
        }
        //public delegate object[] GetDelegate(System.Web.HttpContext context, params object[] args);
        //public delegate object MyDelegate(System.Web.HttpContext context, object args);
        public void BeginDoCallback(object objparams, System.Threading.WaitCallback callBack)
        {
            //与 BeginInvoke 对应 有个 EndInvoke 方法,而且运行完毕返回 IAsyncResult 类型的返回值.
            //这样我们可以这样收集 线程函数的 返回值
            //System.Threading.ThreadPool.UnsafeRegisterWaitForSingleObject(callBack,);
            //MyDelegate dele = new MyDelegate(DoAction);
            //System.Web.HttpContext context = null;
            //try
            //{
            //    context = System.Web.HttpContext.Current;
            //}
            //catch { }
            object[] newobjparams = new object[] { null, objparams };
            System.Threading.ThreadPool.UnsafeQueueUserWorkItem(Wrap(callBack, newobjparams), objparams);

            ////\恢复执行上下文的流动。
            //System.Threading.ThreadPool.UnsafeQueueUserWorkItem(new System.Threading.WaitCallback(( strpSpmtidtem1) => 
            //    {
            //        Console.WriteLine("ThreadPool线程使用Unsafe异步执行方法来取消执行上下文的流动。Name为：\"{0}\""
            //        , CallContext.LogicalGetData("Name"));
            //    }), null  );




            //IAsyncResult refr = dele.BeginInvoke(context,args);
            ////....
            //object result = dele.EndInvoke(refr); //<----收集 返回值
            //return result;
            //int MyFunction(int count, string str); <----带参数和返回值的 线程函数
        } 
         

        /// <summary>
        /// 根据特定识别号码和业务数据ID增加系统临时自动执行事务
        /// </summary>
        /// <param name="strpSpNo">特定识别号码,1自动提醒是否按时完成紧急事务处理</param>
        /// <param name="strpRelId">业务数据ID</param>
        public void AddAutoExecSetBySpNo(string strpSpNo, string strpRelId)
        {
            if (strpSpNo == "1")
            {
            }
        }

        ///// <summary>
        ///// 添加系统临时自动执行事务,CSHARP-程序代码，SQL-SQL语句，SP-存储过程,CFUN-函数调用
        ///// </summary>
        //public void AddAutoExecSet(string strRELATION_USR, string strpLabel, string strpDesc, string strpExecCode, string strpExecCodeType, DateTime dtpPlanExec,bool blNewPlan)
        //{
        //    if (strpLabel == string.Empty)
        //    {
        //        return;
        //    }


        //    using (CDbAccess objtem1 = new CDbAccess())
        //    {
        //        string strSql = "";
        //        objtem1.IdGroup = "RID";
        //        strSql = "INSERT INTO SYS_AUTO_EXEC_SET (AUTO_EXEC_ID," +
        //            "SRC_AUTO_EXEC_ID," +
        //            "AUTO_EXEC_NO," +
        //            "AUTO_EXEC_DESC," +
        //            "EXEC_SQL," +
        //            "NEXT_EXEC_DT," +
        //            "RELATION_USR," +
        //            "REPORT_URL," +
        //            "EXEC_CODE," +
        //            "REC_ACTIVE," +
        //            "EXEC_TYPE_CID," +
        //            "EXEC_CODE_TYPE_CID," +
        //            "AUTO_EXEC_LABEL," +
        //            "EXEC_TIMES ) values (N'" + objtem1.NewID() + "'," +
        //            "N'" + "" + "'," +
        //            "N'" + "" + "'," +
        //            "@strpDesc," +
        //            "@strEXEC_SQL," +
        //            "N'" + dtpPlanExec.ToString() + "'," +
        //            "N'" + strRELATION_USR + "'," +
        //            "N'" + "" + "'," +
        //            "@strpExecCode," +
        //            "1," +
        //            "N'" + "3" + "'," +
        //            "N'" + strpExecCodeType + "'," +
        //            "N'" + strpLabel + "'," +
        //            "0 )";
        //        if (!string.IsNullOrEmpty(strpLabel) && !blNewPlan)
        //        {
        //            DataSet dstm = objtem1.QueryBySQL("SELECT * FROM SYS_AUTO_EXEC_SET WHERE AUTO_EXEC_LABEL='" + strpLabel + "'");
        //            if (dstm.Tables[0].Rows.Count > 0)
        //            {
        //                strSql = "UPDATE SYS_AUTO_EXEC_SET SET  " +
        //                    "AUTO_EXEC_DESC=@strpDesc" +
        //                    ",EXEC_SQL=@strEXEC_SQL" +
        //                    ",NEXT_EXEC_DT=N'" + dtpPlanExec.ToString() + "'" +
        //                    ",RELATION_USR=N'" + strRELATION_USR + "'" +
        //                    ",EXEC_CODE=@strpExecCode" +
        //                    ",EXEC_CODE_TYPE_CID=N'" + strpExecCodeType + "'" +
        //                    " WHERE AUTO_EXEC_LABEL='" + strpLabel + "'";
        //            }
        //        }
        //        CDbParmCollection parmCollection = new CDbParmCollection();
        //        parmCollection.Add(new CDbParm("strpDesc", ParameterDirection.Input, EDbType.String, 200, strpDesc));
        //        parmCollection.Add(new CDbParm("strEXEC_SQL", ParameterDirection.Input, EDbType.String, 10240000, ""));
        //        parmCollection.Add(new CDbParm("strpExecCode", ParameterDirection.Input, EDbType.String, 10240000, strpExecCode)); 
        //        objtem1.ExecuteBySQL(strSql, parmCollection); 
        //    }

        //}

    }
}
